package com.woniuxy.orderassembly.util.Publisher;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.woniuxy.common.Result;
import com.woniuxy.orderassembly.config.Constants;
import com.woniuxy.orderassembly.model.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Component
public class MQAddOrder implements IMQAddOrder {

    @Override
    public Result addRoder(Order order,String routingKing) throws IOException, TimeoutException {


            //建立信道
            Channel channel = ConnUtils.connection();
            //(1)创建交换机,类型是DIRECT
            channel.exchangeDeclare(Constants.ROUTE_EXCHANGE, BuiltinExchangeType.DIRECT);
            String message = order.toString();

            String routingKey = routingKing;

            //(2) 发给交换机，提供routing-key
            channel.basicPublish(Constants.ROUTE_EXCHANGE, routingKey, null, message.getBytes("UTF-8"));


        return Result.success("下单信息已发送，请耐心等待商家回复");
    }
}
